package com.jarpackage.ui;

import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ProgressDialog extends Dialog<Void> {
    private final ProgressBar progressBar;
    private final Label statusLabel;
    private final Button cancelButton;

    public ProgressDialog(Stage owner, String title) {
        initOwner(owner);
        setTitle(title);

        // 创建进度条和状态标签
        progressBar = new ProgressBar(0);
        progressBar.setPrefWidth(300);
        statusLabel = new Label("准备下载...");

        // 创建取消按钮
        cancelButton = new Button("取消");
        cancelButton.setStyle("-fx-background-color: #dc3545; -fx-text-fill: white; -fx-font-size: 12px; -fx-padding: 5 15;");
        cancelButton.setOnAction(e -> close());

        // 设置布局
        VBox content = new VBox(10);
        content.setPadding(new Insets(20));
        content.setAlignment(Pos.CENTER);

        HBox buttonBox = new HBox(10);
        buttonBox.setAlignment(Pos.CENTER);
        buttonBox.getChildren().add(cancelButton);

        content.getChildren().addAll(progressBar, statusLabel, buttonBox);

        // 设置对话框内容
        getDialogPane().setContent(content);
        getDialogPane().setPrefWidth(350);
        getDialogPane().setPrefHeight(150);

        // 设置对话框可关闭
        setOnCloseRequest(e -> close());
    }

    public void updateProgress(double progress) {
        Platform.runLater(() -> progressBar.setProgress(progress));
    }

    public void updateStatus(String status) {
        Platform.runLater(() -> statusLabel.setText(status));
    }
}